package work.mediway.bi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mediway.hos.app.core.util.SecurityUtils;
import com.mediway.hos.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import work.mediway.bi.constant.BiDesignConstant;
import work.mediway.bi.mapper.InterfaceOperMapper;
import work.mediway.bi.model.dto.InterfaceOperDTO;
import work.mediway.bi.model.entity.InterfaceOper;
import work.mediway.bi.model.vo.InterfaceOperVO;
import work.mediway.bi.service.InterfaceOperService;

import java.util.List;
/**
  *@version
  *@description      
  *@author          bovane <bovane.ch@gmail.com>
  *@create          2023-06-13 16:50
 */

@Service
@Slf4j
public class InterfaceOperServiceImpl extends ServiceImpl<InterfaceOperMapper, InterfaceOper> implements InterfaceOperService{

    @Override
    public void deleteinterfaceOper(String ids) {
        List<String> id = StrUtil.split(ids, BiDesignConstant.DEFAULT_SPLIT);
        this.removeBatchByIds(id);
    }

    @Override
    public Boolean updateInterfaceOper(InterfaceOper interfaceOper) {
        // 设置更新人、更新时间
        interfaceOper.setUpdateBy(SecurityUtils.getLoginUser().getUserName());
        interfaceOper.setUpdateTime(DateUtil.date());
        return this.updateById(interfaceOper);
    }

    @Override
    public Boolean addInterfaceOper(InterfaceOper interfaceOper) {
        // 设置默认启用
        if (ObjectUtil.isEmpty(interfaceOper.getActived())) {
            interfaceOper.setActived(Boolean.TRUE);
        }
        // 查看UUID是否重复
        InterfaceOper one = this.getOne(new LambdaQueryWrapper<InterfaceOper>()
                .eq(InterfaceOper::getInterfaceCode, interfaceOper.getInterfaceCode()));
        if (ObjectUtil.isNotEmpty(one)) {
            throw new GlobalException(String.format("当前界面UUID已经存在【{}】", interfaceOper.getInterfaceCode()));
        }
        // 设置创建人、创建时间
        interfaceOper.setCreateBy(SecurityUtils.getLoginUser().getUserName());
        interfaceOper.setCreateTime(DateUtil.date());
        return this.save(interfaceOper);
    }

    @Override
    public IPage<InterfaceOperVO> getInterfaceOper(InterfaceOperDTO interfaceOperDTO) {
        IPage<InterfaceOper> page = new Page<>(interfaceOperDTO.getCurrent(),interfaceOperDTO.getSize());
        List<InterfaceOper> result = this.baseMapper.selectInterfaceOper(page,interfaceOperDTO);
        IPage<InterfaceOperVO> page1 = new Page<>(interfaceOperDTO.getCurrent(),interfaceOperDTO.getSize());
        List<InterfaceOperVO> interfaceOperVOS = InterfaceOperVO.convert(result);
        log.info(result.toString());
        page1.setRecords(interfaceOperVOS);
        return page1;
    }
}
